import axios from 'axios'

const api = axios.create({
  baseURL: '/api',
  timeout: 5000
})

// 获取用户积分
export const getPoints = () => {
  return api.get('/points').then(res => res.data)
}

// 获取积分记录
export const getPointsRecords = () => {
  return api.get('/points/records').then(res => res.data)
}

// 获取商品列表
export const getProducts = () => {
  return api.get('/products').then(res => res.data)
}

// 获取商品详情
export const getProductDetail = (id) => {
  return api.get('/products/' + id).then(res => res.data)
}

// 创建订单
export const createOrder = (data) => {
  return api.post('/orders', data).then(res => res.data)
}

// 获取订单详情
export const getOrderDetail = (id) => {
  return api.get('/orders/' + id).then(res => res.data)
}

// 获取订单列表
export const getOrders = () => {
  return api.get('/orders').then(res => res.data)
}

// 取消订单
export const cancelOrder = (id) => {
  return api.post('/orders/' + id + '/cancel').then(res => res.data)
}

// 获取收货地址列表
export const getAddresses = () => {
  return api.get('/addresses').then(res => res.data)
}

// 添加收货地址
export const addAddress = (data) => {
  return api.post('/addresses', data).then(res => res.data)
}

// 更新收货地址
export const updateAddress = (id, data) => {
  return api.put('/addresses/' + id, data).then(res => res.data)
}

// 删除收货地址
export const deleteAddress = (id) => {
  return api.delete('/addresses/' + id).then(res => res.data)
}

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加token等认证信息
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response
  },
  error => {
    // 统一错误处理
    console.error('请求失败:', error)
    return Promise.reject(error)
  }
) 